package com.goldsprite.gameframeworks.ecs;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;
import com.goldsprite.gameframeworks.assets.GameAssetManager;
import com.goldsprite.gameframeworks.assets.GameRes;
import com.goldsprite.gameframeworks.assets.MTexture;
import com.goldsprite.gameframeworks.utils.math.Vector2Int;

public class SpriteUtils {
	private static int uvX, uvY;

	public static void setUVUnit(int[] xy) {
		setUVUnit(xy[0], xy[1]);
	}

	public static void setUVUnit(int x, int y) {
		uvX = x;
		uvY = y;
	}

	public static Array<TextureRegion> splitFrames(int uvX, int uvY, GameRes res, int row, int start, int count, int mode) {
		setUVUnit(uvX, uvY);
		return splitFrames(res, row, start, count, mode);
	}

	public static Array<TextureRegion> splitFrames(int uvX, int uvY, GameRes res, int col, int count) {
		setUVUnit(uvX, uvY);
		return splitFrames(res, col, count);
	}

	public static Array<TextureRegion> splitFrames(GameRes res, int col, int count) {
		return splitFrames(res, col, 0, count, 0);
	}

	/**
	 * @param res
	 * @param row
	 * @param start
	 * @param count
	 * @param mode  0为切分整行 1为切割单张指定行列图
	 * @return
	 */
	public static Array<TextureRegion> splitFrames(GameRes res, int row, int start, int count, int mode) {
		MTexture hero_sheet = GameAssetManager.getInstance().getTexture(res);
		Array<TextureRegion> frames;
		try (Vector2Int cell = Vector2Int.obtain(uvX, uvY)) {
			frames = new Array<>();
			if (mode == 0) {
				for (int i = 0; i < count; i++) {
					frames.add(new TextureRegion(hero_sheet, (i + start) * cell.intX, row * cell.intY, cell.intX, cell.intY));
				}
			}
			if (mode == 1) {
				frames.add(new TextureRegion(hero_sheet, count * cell.intX, row * cell.intY, cell.intX, cell.intY));
			}
		}
		return frames;
	}


	public static Array<TextureRegion> splitAllFrames(GameRes res, int uvX, int uvY, int xSize, int ySize, int totalPiece) {
		Array<TextureRegion> arrs = new Array<>();
		Texture tex = res.getResource();
		for(int y = 0, tick=0; y < ySize; y++) {
			for(int x = 0; x < xSize; x++, tick++) {
				if(tick >= totalPiece) break;
				TextureRegion region = new TextureRegion(tex, x * uvX, y * uvY, uvX, uvY);
				arrs.add(region);
			}
		}

		return arrs;
	}
	public static Array<TextureRegion> splitAllFrames(GameRes res, int xSize, int ySize, int totalPiece) {
		return splitAllFrames(res, res.getUVUnit()[0], res.getUVUnit()[1], xSize, ySize, totalPiece);
	}
}
